package com.jmxcfc.blfsc.ssq.service.contract.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmxcfc.blfsc.ssq.entity.UploadContractSub;
import com.jmxcfc.blfsc.ssq.mapper.UploadContractSubMapper;
import com.jmxcfc.blfsc.ssq.service.contract.IUploadContractSubService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 上上签上传并创建合同记录表 服务实现类
 * </p>
 *
 * @author pbs
 * @since 2024-09-08
 */
@Service
@Slf4j
public class UploadContractSubServiceImpl extends ServiceImpl<UploadContractSubMapper, UploadContractSub> implements IUploadContractSubService {

    /**
     * 根据业务号查询最新签署合同的id
     * @param requestNo
     * @return
     */
    @Override
    public String queryLatestContractId(String requestNo) {
        LambdaQueryWrapper<UploadContractSub> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(UploadContractSub::getContractId).eq(UploadContractSub::getRequestNo, requestNo)
                .orderByDesc(UploadContractSub::getCreateTime);
        List<UploadContractSub> uploadContractSubList = this.list(queryWrapper);
        String contractId = "";
        if (CollectionUtils.isNotEmpty(uploadContractSubList) && StringUtils.isNotBlank(uploadContractSubList.get(0).getContractId())) {
            contractId =  uploadContractSubList.get(0).getContractId();
        }
        return contractId;
    }
}
